<template>
  <!-- 跟进记录 -->
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="add"
          v-auth-button="['add/followRecord']"
        ></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <el-input
            v-model="searchValue.search"
            placeholder="跟进名称"
            clearable
          ></el-input>
          <el-select
            v-if="userList.length != 0"
            v-model="searchValue.adminId"
            placeholder="请选择创建人"
            style="width: 100%"
            clearable
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="upsearch"
          ></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        row-key="id"
        stripe
        @filterType="filterType"
        @row-dblclick="handleRowDblclick"
      >
        <!-- <el-table-column label="ID" prop="id" width="50"></el-table-column> -->
        <!-- <el-table-column
          label="跟进名称"
          prop="followName"
          :show-overflow-tooltip="true"
          min-width="250"
        ></el-table-column> -->
        <el-table-column
          label="客户名称"
          prop="customerName"
          min-width="200"
          fixed
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="跟进名称"
          prop="followName"
          :show-overflow-tooltip="true"
          min-width="250"
        ></el-table-column>
        <el-table-column label="跟进类型" prop="typeName" min-width="200">
          <!-- 0电话回访，1线下拜访，2线上联系 -->
        </el-table-column>
        <el-table-column
          label="跟进内容"
          prop="content"
          min-width="200"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="跟进进度"
          prop="progress"
          width="150"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column label="下次联系时间" prop="nextTime" width="150">
        </el-table-column>
        <!-- <el-table-column label="审核状态" prop="status" width="200" sortable>
          <template #default="scope">
            <p v-if="scope.row.status == 0">未审核</p>
            <p v-if="scope.row.status == 1" style="color: #67c23a">通过</p>
            <p v-if="scope.row.status == 2" style="color: #f56c6c">未通过</p>
            <p v-if="scope.row.status == 3">审核中</p>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" fixed="right" align="right" width="200">
          <template #default="scope">
            <el-button-group>
              <el-button
                text
                type="primary"
                size="small"
                @click="table_show(scope.row, scope.$index)"
                v-auth-button="['show/followRecord']"
                >查看</el-button
              >
              <el-button
                text
                type="primary"
                size="small"
                @click="table_edit(scope.row, scope.$index)"
                v-auth-button="['edit/followRecord']"
                >编辑</el-button
              >
              <el-popconfirm
                title="确定删除吗？"
                @confirm="table_del(scope.row, scope.$index)"
              >
                <template #reference>
                  <el-button
                    text
                    type="primary"
                    size="small"
                    v-auth-button="['delete/followRecord']"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <save-dialog
    v-if="dialog.save"
    ref="saveDialog"
    @success="handleSaveSuccess"
    @closed="dialog.save = false"
  ></save-dialog>
</template>

<script>
import saveDialog from './components/save'
import { queryTypeByDic } from '@/utils/getData'
export default {
  name: 'recordList',
  components: {
    saveDialog,
  },
  data () {
    return {
      dialog: {
        save: false,
      },
      apiObj: this.$API.contract.followRecord.getFollowRecords,
      selection: [],
      userList: [],
      searchValue: {
        search: null,
        adminId: null
      }
    }
  },
  mounted () {
    this.userInfo = this.$TOOL.data.get('USER_INFO')
    this.getUserList()
  },
  methods: {
    // 获取所有用户列表
    async getUserList () {
      let res = await this.$API.user.getAdminList.get()
      this.userList = res.data
    },
    //添加
    add () {
      // this.dialog.save = true
      // this.$nextTick(() => {
      //   this.$refs.saveDialog.open()
      // })
      this.$router.push({
        path: '/customer/followRecord/followRecordAdd',
        query: {
          mode: 'add'
        }
      })
    },
    //编辑
    table_edit (row) {
      let str = row.status == 3 ? '审核中' : '已通过审核'
      if (row.status !== 0) {
        this.$message.error(`跟踪记录${str}，不能编辑`)
        return
      }
      this.$router.push({
        path: '/customer/followRecord/followRecordAdd',
        query: {
          mode: 'edit',
          id: row.id
        }
      })
    },
    //查看
    table_show (row) {
      this.$router.push({
        path: '/customer/followRecord/followRecordDetail',
        query: {
          id: row.id
        }
      })
    },

    //删除
    async table_del (row) {
      let str = row.status == 3 ? '审核中' : '已通过审核'
      if (row.status !== 0) {
        this.$message.error(`跟踪记录${str}，不能删除`)
        return
      }
      var reqData = { id: row.id }
      var res = await this.$API.contract.followRecord.deleteFollowRecord.post(reqData)
      if (res.code == 200) {
        this.$refs.table.refresh()
        this.$message.success("删除成功")
      }
    },

    //搜索
    upsearch () {
      this.$refs.table.upData(this.searchValue)
    },

    //本地更新数据
    handleSaveSuccess (data, mode) {
      if (mode == 'add') {
        this.$refs.table.refresh()
      } else if (mode == 'edit') {
        this.$refs.table.refresh()
      }
    },
    // 处理跟进类型
    filterType (data) {
      let tableData = data
      queryTypeByDic('followType').then(res => {
        let list = res
        tableData.forEach(item => {
          list.forEach(f_item => {
            if (item.type == f_item.value) {
              item.typeName = f_item.name
            }
          })
        })
      })
    },
    // 双击表格
    handleRowDblclick (row) {
      this.table_show(row)
    }
  }
}
</script>

<style>
</style>

